package com.iee.concurrent.thread.completablefuture;

import java.util.concurrent.*;

/**
 *@ClassName b
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2021/4/18 0018 21:34
 */
public class b {

    private static Executor executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        test3();
    }

    //第二个任务是返回CompletableFuture的异步方法
    CompletableFuture<String> getDeal(String s){
        return CompletableFuture.supplyAsync(() ->  s+"long");
    }

    public static void test3(){
        //任务1：洗水壶->烧开水, 无返回值
//        CompletableFuture f1 =
//                CompletableFuture.runAsync(()->{
//                    System.out.println("T1:洗水壶...");
//                    sleep(1, TimeUnit.SECONDS);
//
//                    System.out.println("T1:烧开水...");
//                    sleep(15, TimeUnit.SECONDS);
//                });
        //任务1：洗水壶->烧开水， 有返回值
        CompletableFuture f11 =
                CompletableFuture.supplyAsync(()->{
                    System.out.println("T1:洗水壶...");
                    sleep(1, TimeUnit.SECONDS);
//                    int i=1/0;
                    System.out.println("T1:烧开水...");
                    sleep(15, TimeUnit.SECONDS);
                    return "西湖水";
                });
        //任务2：洗茶壶->洗茶杯->拿茶叶
        CompletableFuture f2 =
                CompletableFuture.supplyAsync(()->{
                    System.out.println("T2:洗茶壶...");
                    sleep(1, TimeUnit.SECONDS);

                    System.out.println("T2:洗茶杯...");
                    sleep(2, TimeUnit.SECONDS);

                    System.out.println("T2:拿茶叶...");
                    sleep(1, TimeUnit.SECONDS);
//                    return "龙井";
                    return 11;
                });
        //任务3：任务1和任务2完成后执行：泡茶
//        CompletableFuture f3 =
//                f1.thenCombine(f2, (__, b)->{
//                    System.out.println("T3:拿到开水:" + __);  // __ 的值为null
//                    System.out.println("T3:拿到茶叶:" + b);
//                    System.out.println("T3:泡茶...");
//                    return "上茶:" + __ + b;
//                });
        CompletableFuture f3 =
                f11.thenCombine(f2, (a, b)->{
                    System.out.println("T3:拿到开水:" + a);
                    System.out.println("T3:拿到茶叶:" + b);
                    int i=1/0;
                    System.out.println("T3:泡茶...");
                    return "上茶:" + a + b;
                }).exceptionally(ex -> {
                    throw new RuntimeException("发生异常，请求回滚");
//                    return "Unknown!";
                });
        System.out.println(f3.join());

        //等待任务3执行结果
//        String s = CompletableFuture.allOf(f11, f2).thenApply(__ -> {
//            System.out.println("T3:拿到开水和茶叶");
//            //            System.out.println("T3:拿到茶叶:" + b);
//            System.out.println("T3:泡茶...");
//            return "上茶，狗屎茶";
//        }).exceptionally(ex -> {
//            System.out.println("Oops! We have an exception - " + ex.getMessage());
//            return "Unknown!";
//        }).join();
//        System.out.println(s);

    }

    public static void sleep(int t, TimeUnit u) {
        try {
            u.sleep(t);
        }catch(InterruptedException e){}
    }



}
